<?php
$docRoot = $_SERVER ['DOCUMENT_ROOT'] . '/' . strtok ( $_SERVER ['PHP_SELF'], '/' );
include_once ($docRoot . "/application/service/feed/loader/FeedLoader.php");
include_once ($docRoot . "/application/service/db/TeamLoaderDAO.php");
?>
<?php

/** 
 * @author Bibek
 * 
 * 
 */
class TeamLoaderImpl implements FeedLoader {
	
	private $rostersFeedUrl;
	private $statsFeedUrl;
	
	private $rostersFeedXml;
	private $statsFeedXml;
	private $feedTeamXmlPath;
	
	/**
	 * @param field_type $feedTeamXmlPath
	 */
	public function setFeedTeamXmlPath($feedTeamXmlPath) {
		$this->feedTeamXmlPath = $feedTeamXmlPath;
	}
	
	/**
	 * @param field_type $rostersFeedXml
	 */
	public function setRostersFeedXml($rostersFeedXml) {
		$this->rostersFeedXml = $rostersFeedXml;
	}
	
	/**
	 * @param field_type $statsFeedXml
	 */
	public function setStatsFeedXml($statsFeedXml) {
		$this->statsFeedXml = $statsFeedXml;
	}
	
	public function setRostersFeedUrl($rostersFeedUrl) {
		$this->rostersFeedUrl = $rostersFeedUrl;
	}
	
	public function setStatsFeedUrl($statsFeedUrl) {
		$this->statsFeedUrl = $statsFeedUrl;
	}
	
	public function loadFeed() {
		$isSuccess = false;
		
		//1. get team name form rostersFeedUrl
		//2. Save these two feed urls in proper location defined in site config
		//3. populate database from feedurl
		

		$rostersFeedContent = file_get_contents ( $this->rostersFeedUrl );
		$statsFeedContent = file_get_contents ( $this->statsFeedUrl );
		
		$rostersXmlObject = simplexml_load_string ( $rostersFeedContent );
		
		$attributeTeamId = "id";
		$attributeTeamName = "name";
		
		$teamId = $rostersXmlObject->attributes ()->$attributeTeamId;
		$teamName = $rostersXmlObject->attributes ()->$attributeTeamName;
		
		mkdir ( $this->feedTeamXmlPath . $teamId, 0700 );
		
		$rostersFeedSavePath = $this->feedTeamXmlPath . $teamId . $this->rostersFeedXml;
		$statsFeedSavePath = $this->feedTeamXmlPath . $teamId . $this->statsFeedXml;
		
		$fh = fopen ( $rostersFeedSavePath, 'w' );
		fwrite ( $fh, $rostersFeedContent );
		fclose ( $fh );
		
		$fh = fopen ( $statsFeedSavePath, 'w' );
		fwrite ( $fh, $statsFeedContent );
		fclose ( $fh );
		
		$teamLoaderDAO = new TeamLoaderDAO();
		//Create a team Array and add to database
		$teamLoaderDAO->addTeam($teamId, $teamName, $this->rostersFeedUrl, $this->statsFeedUrl);

		//creat a player arraly and add to database
		$playersNodeList = $rostersXmlObject->xpath ( "//player" );
		
		$playersArray = array();
		
		
		while ( list ( , $node ) = each ( $playersNodeList ) ) {
			$playersAttribute = $node->attributes ();
			$playerName = (String)$playersAttribute ['name'];
			$playerName = str_replace("'", "&#39;", $playerName);
			$playerId = (String)$playersAttribute ['id'];
			$newPlayer = array("name"=>$playerName,"id"=>$playerId);
			array_push($playersArray, $newPlayer);
		}

		$teamLoaderDAO->addPlayers($playersArray, $teamId);
		
		return $isSuccess;
	}

}

?>